const { zc, ishas, update, updatename, modulebaibai, unlinedata } = require('../../serves/userlogin/index')
const { usersum } = require('../../serves/hose')
const axios = require('axios')
const jwt = require('jsonwebtoken')
module.exports = {
    async userzc(ctx, next) {
        const userdata = {
            username: ctx.query.username,
            password: ctx.query.password
        }
        await zc(userdata)
        ctx.body = {
            code: 200,
            msg: '注册成功！'
        }
    },
    async userlogin(ctx, next) {
        const data = await usersum(ctx.id)
        ctx.body = {
            code: 200,
            msg: "登录成功！",
            token: ctx.token,
            userdata: {
                ...ctx.userdata,
                ...data
            },
        }
    }
    ,
    async sdlogin(ctx, next) {
        const { code, provider, userinfo } = ctx.request.body
        const { nickName: username, avatarUrl: img } = userinfo
        if (provider == "weixin") {
            const wxdata = await axios.get("https://api.weixin.qq.com/sns/jscode2session", { params: { appid: 'wxea7ed276f4bc2c27', secret: "8151e77602ce04eae23e3c4c34ea8fb9", js_code: code, grant_type: "authorization_code" } })
            const hasdata = await ishas({ openid: wxdata.data.openid })
            if (hasdata) {
                const datass = await usersum(hasdata.id)
                const { password, ...datas } = hasdata.dataValues
                return ctx.body = {
                    code: 200,
                    msg: "登陆成功！",
                    token: jwt.sign(datas, "ximo", { expiresIn: "1days" }),
                    data: {
                        ...datas, ...datass
                    }
                }
            } else {
                await zc({ name: "默认" + username + "名称", img, openid: wxdata.data.openid })
                const hasdata = await ishas({ openid: wxdata.data.openid })
                const datass = await usersum(hasdata.id)
                const { password, ...datas } = hasdata.dataValues
                return ctx.body = {
                    code: 200,
                    msg: "登陆成功！",
                    token: jwt.sign(datas, "ximo", { expiresIn: "1days" }),
                    data: {
                        ...datas, ...datass
                    }
                }
            }
        }
        //qq登录
        // else {
        //     const rudata = await axios.get("https://api.q.qq.com/sns/jscode2session", { params: { appid: 'wxea7ed276f4bc2c27', secret: "8151e77602ce04eae23e3c4c34ea8fb9", js_code: code, grant_type: "authorization_code" } })
        //     console.log(rudata.data)
        //     const { openid } = rudata.data
        //     const hasdata = await ishas({ openid })
        //     if (hasdata) {
        //         const { password, ...data } = hasdata.dataValues
        //         return ctx.body = {
        //             code: 200,
        //             msg: "登陆成功！",
        //             token: jwt.sign(data, "ximo"),
        //             data
        //         }
        //     } else {
        //         await zc({ openid })
        //         const hasdata = await ishas({ openid })
        //         const { password, ...data } = hasdata.dataValues
        //         return ctx.body = {
        //             code: 200,
        //             msg: "登陆成功！",
        //             token: jwt.sign(data, "ximo"),
        //             data
        //         }
        //     }
        // }
    }
    , async findid(ctx, next) {
        const { id } = ctx.params
        const data = await ishas({ id })
        ctx.body = {
            code: 200,
            msg: "ok",
            data
        }
    },
    async updateimage(ctx, next) {
        const id = ctx.id
        const img = ctx.request.files.img.newFilename
        await update(id, img)
        ctx.body = {
            code: 200,
            msg: "ok",
            newimg: img
        }
    },
    async updatename(ctx, next) {
        const id = ctx.id
        const { name } = ctx.request.body
        await updatename(id, name)
        ctx.body = {
            code: 200,
            msg: "ok",
            name
        }
    },
    async userallsum(ctx, next) {
        const rudata = await usersum(ctx.id)
        ctx.body = {
            code: 200,
            msg: "ok",
            rudata
        }
    },
    async baibai(ctx, next) {
        const id = ctx.id
        await modulebaibai(id)
        ctx.body = {
            code: 200,
            msg: "ok"
        }
    },
    async getunlinedata(ctx, next) {
        const id = ctx.id
        const data = await unlinedata(id)
        ctx.body = {
            code: 200,
            data
        }
    }
}